package com.chb.controller;


import com.chb.service.PaymentHytrixService;
import com.chb.service.PaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@DefaultProperties(defaultFallback = "paymentInfo_global_FallbackHandler",commandProperties = {
          @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")})
public class OrderHytrixController {

    @Autowired
    private PaymentHytrixService paymentHytrixService;




    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    String paymentInfo_ok(@PathVariable("id") Integer id){
        return paymentHytrixService.paymentInfo_ok(id);
    }
//    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
//    String paymentInfo_timeout(@PathVariable("id") Integer id){
//
//        return paymentHytrixService.paymentInfo_timeout(id);
//    }

    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
//    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
//    })
    @HystrixCommand
    public String paymentInfo_Timeout(@PathVariable("id")Integer id)  {

        Integer time = id;
//        int a = 10 /0;
        try {

            TimeUnit.SECONDS.sleep(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "线程池： "+Thread.currentThread().getName()+ "paymentInfo_ok,id: "+id +"耗时"+time+"S";

    }

    public String paymentInfo_TimeOutHandler(Integer id){

        return "我是消费者80，对方支付系统繁忙，请稍后再试";


    }
    public String paymentInfo_global_FallbackHandler(){

        return "Global异常处理，对方支付系统繁忙，请稍后再试";


    }





}
